/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Negocio.Artigo;
import Negocio.CadastroException;
import Persistencia.DAOException;
import javax.swing.JOptionPane;

/**
 *
 * @author asouza
 */
public class FLance extends javax.swing.JFrame {

    /**
     * Creates new form FLance
     */
    Artigo artigo;
    CadastroControlador controlador;
    double UltimoLance = 0;
    
    public FLance(Artigo a) throws CadastroException, DAOException, Exception {
          
        this.artigo = a;
        
        controlador = new CadastroControlador(a);
        initComponents(); 
        
        UltimoLance = controlador.getLanceMinimo(a);
        
      
        if(UltimoLance == 0)
        
            //Recupera o lance inicial do artigo
        UltimoLance = a.getValorInicial();
        
        jLabelValorMínimo.setText(" R$ "+ UltimoLance);
     
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jListLances = new javax.swing.JList();
        jButtonFechar = new javax.swing.JButton();
        jButtonAddLance = new javax.swing.JButton();
        jTextFieldLance = new javax.swing.JTextField();
        jCBUser = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabelValorMínimo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lance");

        jListLances.setModel(controlador.getListaLance());
        jScrollPane1.setViewportView(jListLances);

        jButtonFechar.setText("Fechar");
        jButtonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharActionPerformed(evt);
            }
        });

        jButtonAddLance.setText("Lance");
        jButtonAddLance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddLanceActionPerformed(evt);
            }
        });

        jCBUser.setModel(controlador.getListaUsuarioCombo());

        jLabel1.setText("Usuário Lance:");

        jLabel2.setText("Valor lance:");

        jLabel3.setText("Valor Mínimo:");

        jLabelValorMínimo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelValorMínimo.setText("jLabel4");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButtonFechar))
                            .addComponent(jCBUser, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldLance, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButtonAddLance)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelValorMínimo)
                        .addGap(39, 39, 39))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabelValorMínimo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCBUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldLance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonAddLance))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonFechar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(5, 5, 5))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAddLanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddLanceActionPerformed
        // TODO add your handling code here:
        try {
            String idU = "";
            
            if (jCBUser.getSelectedIndex() != -1) {
                idU = "" + controlador.getListaUsuarioCombo().getElementAt(jCBUser.getSelectedIndex()).getId();
            }
            String valorLance = jTextFieldLance.getText();
            
            if (controlador.adicionarLance(idU, valorLance, artigo.getId(), UltimoLance)) {
                JOptionPane.showMessageDialog(this, "Lance efetuado com sucesso.");  
                UltimoLance = controlador.getLanceMinimo(this.artigo);
                jTextFieldLance.setText("");
                jLabelValorMínimo.setText("R$ "+ UltimoLance);
            }else{
              JOptionPane.showMessageDialog(this, "Não foi possível cadastrar o lance.");
            }

        } catch (CadastroException e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        } catch (DAOException e) {
            JOptionPane.showMessageDialog(this,"Preço Mínimo" + e.getMessage());
        }
    }//GEN-LAST:event_jButtonAddLanceActionPerformed

    private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonFecharActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddLance;
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JComboBox jCBUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelValorMínimo;
    private javax.swing.JList jListLances;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldLance;
    // End of variables declaration//GEN-END:variables
}
